package org.sam.swing.table.verification;

import org.sam.swing.table.JSTableColumn;
import org.sam.swing.table.JSTableModel;

/**
 * 组合模式的校验器 挨个校验
 * 
 * @author sam
 *
 */
public class CompositeVerificatior extends AbstractVerificatior {

	/**
	 * 校验器集合
	 */
	private Verificatior[] verificatiores;

	/**
	 * 校验器集合
	 */
	public Verificatior[] getVerificatiores() {
		return verificatiores;
	}

	/**
	 * 校验器集合
	 */
	public void setVerificatiores(Verificatior[] verificatiores) {
		this.verificatiores = verificatiores;
	}

	/**
	 * 用来缓存错误信息的字符串对象
	 */
	protected StringBuilder sb = new StringBuilder();

	/**
	 * 
	 * @param verificatiores
	 */
	public CompositeVerificatior(Verificatior... verificatiores) {
		super();
		this.setVerificatiores(verificatiores);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setParent(JSTableColumn column) {
		super.setParent(column);
		if (verificatiores != null && verificatiores.length > 0) {
			for (Verificatior v : verificatiores) {
				v.setParent(column);
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean check(JSTableModel<?> model, int row, int column) {
		if (!this.checkInput(model, row, column))
			return false;

		if (verificatiores == null || verificatiores.length <= 0)
			throw new NullPointerException("未设置校验器");

		boolean result = true;
		sb.delete(0, sb.length());

		for (Verificatior v : verificatiores) {
			if (!v.check(model, row)) {
				sb.append(v.getMsg(model, row));
				sb.append("\r\n");
				result = false;
			}
		}

		return result;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean verificat(Object value) {
		if (verificatiores == null || verificatiores.length <= 0)
			throw new NullPointerException("未设置校验器");

		for (Verificatior v : verificatiores) {
			if (!v.verificat(value)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * {@inheritDoc}
	 * 
	 */
	@Override
	public String getMsg(JSTableModel<?> model, int row) {
		return sb.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getMsg(JSTableModel<?> model, int row, int column) {
		return sb.toString();
	}
}
